import heapq


class Solution:
    def getOrder(self, tasks: list) -> list:
        q = []

        for i in range(len(tasks)):
            task = tasks[i]
            heapq.heappush(q, (task[0], task[1], i))
        res = []
        pq = []
        t = 1
        while q:
            nt = t + 1
            while True:
                et, pt, i = heapq.heappop(q)
                if et > t:
                    heapq.heappush(q, (et, pt, i))
                    nt = et
                    break
                heapq.heappush(pq, (pt, i))
                if len(q) == 0:
                    break
            if len(pq) == 0:
                t = nt
            else:
                pt, i = heapq.heappop(pq)
                t += pt
                res.append(i)
        while pq:
            pt, i = heapq.heappop(pq)
            res.append(i)

        return res
